const express = require("express")
const router = express.Router()
const historicalDB = require("../../../db/historical")
const { join } = require("path")
const multer = require("multer")
const path = require("path")

// 获取所有的历史人物
router.get('/all', async (req, res) => {
  try {
    const lists = await historicalDB.find({}, {}, {
      sort: {
        date: -1
      }
    })

    res.send({
      code: 0,
      mes: "历史人物列表",
      data: lists
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})


// 删除操作
router.use("/delete", require("./delete"))

// 更新操作
router.use("/update", require("./update"))

// 发表操作
// 鉴权有没有发表权限
router.use((req, res, next) => {
  if (!req.session.userInfo.allowPublish) {
    return res.send({
      code: 14,
      mes: "您没有发表权限，请联系超级管理员进行添加"
    })
  }
  next()
})

// 上传cover文件
let coverUpload = multer({
  storage: multer.diskStorage({
    // 文件存储的目录
    destination(req, file, cb) {
      cb(null, join(__dirname, "../../../static/historical/cover"))
    },
    // 文件的名字
    filename(req, file, cb) {
      let name = req.session.userInfo._id
      let ext = path.extname(file.originalname)
      req.coverFile = "historical" + name + Date.now() + ext
      cb(null, req.coverFile)  //名字
    }
  })
}).single("file") //name名字 elemetUI默认为file

// 上传md文件
let mdUpload = multer({
  storage: multer.diskStorage({
    // 文件存储的目录
    destination(req, file, cb) {
      cb(null, join(__dirname, "../../../static/historical/md"))
    },
    // 文件的名字
    filename(req, file, cb) {
      let name = req.session.userInfo._id
      let ext = path.extname(file.originalname)
      req.coverFile = "historical" + name + Date.now() + ext
      cb(null, req.coverFile)  //名字
    }
  })
}).single("file") //name名字 elemetUI默认为file

// 上传md文件
router.post("/uploadMd", async (req, res) => {
  try {
    mdUpload(req, res, async err => {
      if (err) {
        return res.send({
          code: 8,
          mes: "md文件上传失败,请刷新后重试"
        })
      }
      res.send({
        code: 0,
        mes: "md文件上传成功",
        data: "/historical/md/" + req.coverFile
      })
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

// 上传cover文件
router.post("/uploadCover", async (req, res) => {
  try {
    coverUpload(req, res, async err => {
      if (err) {
        return res.send({
          code: 8,
          mes: "md文件上传失败,请刷新后重试"
        })
      }
      res.send({
        code: 0,
        mes: "cover文件上传成功",
        data: "/historical/cover/" + req.coverFile
      })
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

// 添加历史人物
router.post('/add', async (req, res) => {
  try {
    const { cover, name, des, md } = req.body

    await historicalDB.create({
      cover,
      name,
      des,
      md
    })

    res.send({
      code: 0,
      mes: "历史人物添加成功"
    })
  } catch (error) {
    res.send({
      code: 5,
      mes: "服务器异常，请稍后再试"
    })
  }
})

module.exports = router